#include <stdio.h>
#include <arpa/inet.h>

int main()
{
    unsigned short host_port = 0x1234;
    unsigned short net_port;
    unsigned long host_addr = 0x12345678;
    unsigned long net_addr;

    //将本地字节序变成网络序
    //一般端口号用s,ip地址用l
    //本机是小端序，所以下面的函数转网络字节序（大端）的时候会反序，但若在本来就是大端序的机器上，则不影响 
    net_port = htons(host_port);
    net_addr = htonl(host_addr);
    printf("HOST ORDERED PORT: %#x \n",host_port);
    printf("NET ORDERED PORT: %#x \n",net_port);
    printf("HOST ORDERED ADDR: %#x \n",host_addr);
    printf("NET ORDERED ADDR: %#x \n",net_addr);
    return 0;
}